package fr.insalyon.sims.broadcast.tests.service;

import java.awt.Dimension;

import javax.swing.JFrame;

import fr.insalyon.sims.broadcast.network.DataErrorEvent;
import fr.insalyon.sims.broadcast.network.DataErrorListener;
import fr.insalyon.sims.broadcast.network.TCPStreamReceiver;
import fr.insalyon.sims.broadcast.tools.DisplayPanel;
import fr.insalyon.sims.broadcast.tools.ImageBuilder;

public class ImageDisplayer {
	// ------------------------------------------------------- CONSTANTS -- //
	// ------------------------------------------------------ ATTRIBUTES -- //
	// ---------------------------------------------------- CONSTRUCTORS -- //
	// --------------------------------------------------------- METHODS -- //
	
	public static void main(String[] args) {
		int srcPort = 22592;
		
		// Command line parameters values
		for (int i = 0; i < args.length; i++) {
			String arg = args[i];
			
			try {
				if (arg.equals("-p") || args.equals("-port"))
					srcPort = Integer.parseInt(args[i + 1]);
			} catch (Exception e) {
				System.out.println("Syntax: -p <src_port>");
				System.exit(0);
			}
		}
		
		// Create image display stuff
		ImageBuilder builder = new ImageBuilder();
		DisplayPanel panel = new DisplayPanel(builder);
		panel.start();
		
		JFrame frame = new JFrame("Display");
		frame.add(panel);
		frame.setSize(new Dimension(600, 500));
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		
		// Create TCP receiver stream
		TCPStreamReceiver receiver = new TCPStreamReceiver(srcPort);
		receiver.addDataListener(builder);
		receiver.addDataErrorListener(new DataErrorListener() {
			public void streamError(DataErrorEvent event) {
				System.out.println("[receiver] an exception occured!");
				event.getException().printStackTrace();
			}
		});

		// Start the stream
		receiver.connect();
		receiver.start();
		System.out.println("[receiver] started");
	}
	
	// ------------------------------------------------- PRIVATE METHODS -- //
	// --------------------------------------------------- INNER CLASSES -- //
}
